/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.candidat;

import gestionform.GestionCandidatures;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Candidat;
import model.Candidature;
import utils.DateUtilsException;

/**
 *
 * @author Paul
 */
public class ServletCandidatures extends HttpServlet {

    private final String VUE_CONSULTATION_CANDIDATURES = "view/consultationCandidatures.jsp";
    private final String ATT_GESTION_CANDIDATURES = "gCandidatures";

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Candidat c = (Candidat) request.getSession().getAttribute("candidat");
        if (c != null) {
            if (request.getSession().getAttribute("listeCandidatures") == null) {
                HttpSession session = request.getSession(true);
                GestionCandidatures gCandidatures = new GestionCandidatures(c);
                List<Candidature> listeCandidatures;
                try {
                    listeCandidatures = gCandidatures.getListeCandidatures();
                    session.setAttribute("listeCandidatures", listeCandidatures);
                    session.setAttribute("nomFormation", gCandidatures.getNomFormation());
                    session.setAttribute("candidatInfos", c);
                } catch (DateUtilsException ex) {
                    Logger.getLogger(ServletCandidatures.class.getName()).log(Level.SEVERE, null, ex);
                }
                request.setAttribute(ATT_GESTION_CANDIDATURES, gCandidatures);
            }
            request.getRequestDispatcher(VUE_CONSULTATION_CANDIDATURES).forward(request, response);

        } else {
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Candidat c = (Candidat) request.getSession().getAttribute("candidat");
        if (c != null) {
            if (request.getSession().getAttribute("listeCandidatures") == null) {
                HttpSession session = request.getSession(true);
                GestionCandidatures gCandidatures = new GestionCandidatures(c);
                List<Candidature> listeCandidatures;
                try {
                    listeCandidatures = gCandidatures.getListeCandidatures();
                    session.setAttribute("listeCandidatures", listeCandidatures);
                    
                } catch (DateUtilsException ex) {
                    Logger.getLogger(ServletCandidatures.class.getName()).log(Level.SEVERE, null, ex);
                }
                request.setAttribute(ATT_GESTION_CANDIDATURES, gCandidatures);
            }
            request.getRequestDispatcher(VUE_CONSULTATION_CANDIDATURES).forward(request, response);

        } else {
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
    }
}
